package com.mxy.myapplication.asynctask;

import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Message;

import com.mxy.myapplication.util.Util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 压缩图片
 */

public class AsyncTaskForCompressPhoto extends AsyncTask<Integer, Integer, Integer> {

    private String photopath;
    private Message m;

    public AsyncTaskForCompressPhoto(String photopath, Message m) {
        this.photopath = photopath;
        this.m = m;
    }

    @Override
    protected Integer doInBackground(Integer[] params) {
        try {
            Util.compressPhoto(new File(photopath));
            BufferedInputStream bufferedInputStream =
                    new BufferedInputStream(new FileInputStream(new File(photopath)));
            m.obj = BitmapFactory.decodeStream(bufferedInputStream);
            bufferedInputStream.close();
            return 0;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    protected void onPostExecute(Integer o) {
        m.arg1 = (Integer) o;
        m.sendToTarget();
    }
}